package com.fast.cron.db.dao;

import com.fast.cron.db.DbHelperFactory;
import com.fast.cron.db.DbHelperFactoryImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author :leo.jie
 * @version :v1.0
 * @date :2018/9/29 14:51
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class AbstractDao {
    private DbHelperFactory dbHelperFactory;

    private void init() {
        if (null == dbHelperFactory) {
            dbHelperFactory = new DbHelperFactoryImpl();
        }
    }

    public AbstractDao() {
        super();
        init();
    }

    public AbstractDao(DbHelperFactory dbHelperFactory) {
        super();
        this.dbHelperFactory = dbHelperFactory;
        init();
    }

    protected List<Map<String, Object>> query(String sql, Object... para) throws SQLException {
        return dbHelperFactory.query(sql, para);
    }

    protected <T> List<T> query(Class<T> cls, String sql, Object... para) throws SQLException {
        return dbHelperFactory.query(cls, sql, para);
    }

    protected <T> T queryOne(Class<T> cls, String sql, Object... para) throws SQLException {
        return dbHelperFactory.queryOne(cls, sql, para);
    }

    protected <T> List<T> query(Class<T> cls, String sql, Map rowMap, Object... para) throws SQLException {
        return dbHelperFactory.query(cls, sql, rowMap, para);
    }

    protected <T> T queryOne(Class<T> cls, String sql, Map rowMap, Object... para) throws SQLException {
        return dbHelperFactory.queryOne(cls, sql, rowMap, para);
    }

    protected Long queryCount(String sql, Object... para) throws SQLException {
        return dbHelperFactory.queryCount(sql, para);
    }

    protected int update(String sql, Object... para) throws SQLException {
        return dbHelperFactory.update(sql, para);
    }

    protected Long insert(String sql, Object... para) throws SQLException {
        return dbHelperFactory.insert(sql, para);
    }

    protected void executeBach(String sql, Object[][] params) throws SQLException {
        dbHelperFactory.executeBach(sql, params);
    }

    protected void startTransaction() throws SQLException {
        dbHelperFactory.startTransaction();
    }

    protected void commit() throws SQLException {
        dbHelperFactory.commit();
    }

    protected void rollback() throws SQLException {
        dbHelperFactory.rollback();
    }
}
